package com.baobao.service;

import com.baobao.dao.SchoolDAO;

import com.baobao.domain.School;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for School entities
 * 
 */

@Service("SchoolService")
@Transactional
public class SchoolServiceImpl implements SchoolService {

	/**
	 * DAO injected by Spring that manages School entities
	 * 
	 */
	@Autowired
	private SchoolDAO schoolDAO;

	/**
	 * Instantiates a new SchoolServiceImpl.
	 *
	 */
	public SchoolServiceImpl() {
	}

	/**
	 * Save an existing School entity
	 * 
	 */
	@Transactional
	public void saveSchool(School school) {
		School existingSchool = schoolDAO.findSchoolByPrimaryKey(school.getId());

		if (existingSchool != null) {
			if (existingSchool != school) {
				existingSchool.setId(school.getId());
				existingSchool.setProvince(school.getProvince());
				existingSchool.setCity(school.getCity());
				existingSchool.setCounty(school.getCounty());
				existingSchool.setName(school.getName());
				existingSchool.setAddress(school.getAddress());
				existingSchool.setPostcode(school.getPostcode());
				existingSchool.setPhone(school.getPhone());
			}
			school = schoolDAO.store(existingSchool);
		} else {
			school = schoolDAO.store(school);
		}
		schoolDAO.flush();
	}

	/**
	 * Return a count of all School entity
	 * 
	 */
	@Transactional
	public Integer countSchools() {
		return ((Long) schoolDAO.createQuerySingleResult("select count(o) from School o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public School findSchoolByPrimaryKey(String id) {
		return schoolDAO.findSchoolByPrimaryKey(id);
	}

	/**
	 * Return all School entity
	 * 
	 */
	@Transactional
	public List<School> findAllSchools(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<School>(schoolDAO.findAllSchools(startResult, maxRows));
	}

	/**
	 * Load an existing School entity
	 * 
	 */
	@Transactional
	public Set<School> loadSchools() {
		return schoolDAO.findAllSchools();
	}

	/**
	 * Delete an existing School entity
	 * 
	 */
	@Transactional
	public void deleteSchool(School school) {
		schoolDAO.remove(school);
		schoolDAO.flush();
	}
}
